"use strict";
define(["jquery", "template"], function ($, template) {
    function _toConsumableArray(arr) {
        if (Array.isArray(arr)) {
            for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
                arr2[i] = arr[i];
            }
            return arr2;
        } else {
            return Array.from(arr);
        }
    }

    (function ($) {
        var SideNav = function () {
            function SideNav(el, options) {
                this.id = "sideNav" + Math.round(Math.random() * 1000000000); //实例唯一ID名
                this.defalut = {
                    sysName: "", //{string}组件主标题
                    theme: 0, //{number}组件主题参数,支持【0-7】
                    size: "normal", //{string} normal｜small｜top
                    currentValue: "", //{string}默认选中项的id
                    currentOpen: true, //选中是否展开
                    data: [], //数据
                    extend: "" //{string|function} 在组件中的拓展接口,可传dom字符和以fc返回
                };
                //合并默认参数与入参
                this._set = $.extend(this._defaluts, options);
                this.systemId = this._set.systemId;
                this.settings = $.extend(true, this.defalut, options, {
                    id: this.id
                } || []);
                //注册选择元素在组件属性中
                this.el = el;
                //注册组件被选中时的数据对象
                this.active = {
                    "data": null
                };
                //执行初始化操作
                this.init();
            };
            SideNav.prototype = {
                //执行初始化操作
                init: function init() {
                    this.initHtml(); //填充组件结构
                    this.initAttribute(); //设置组件属性
                    this.setPosition(); //集中初始化子菜单ui位置
                    this.watchAction(); //监听组件状态
                    this.initEvens(); //注册组件事件
                    this.watchActive(); //监听组件被选中时的数据对象
                    this.settings.currentValue ? this.setActive(this.settings.currentValue) : ""; //设置组件的默认选中项
                    if (this.settings.data) {
                        this.watchWidth(this.settings.data.length); //监听顶部菜单在距离不够时出现更多按钮
                    }
                },
                //设置组件属性
                initAttribute: function initAttribute() {
                    //设置组件的默认主题
                    $(this.el).addClass("sideNav").attr("theme", this.settings.theme);
                    //设置组件的唯一ID
                    $(this.el).attr("data-navId", this.settings.id);
                    //转化数据结构
                    if (this.settings.data) {
                        this.dataMap = arraytomap(flatten(this.settings.data));
                    }
                    if (this.settings.size === "small") {
                        $(this.el).attr("nav-sm", "");
                    }
                    var sizeSet = ["normal", "small", "top"];
                    //判断组件尺寸是否符合要求
                    if (sizeSet.indexOf(this.settings.size) > -1) {
                        $(this.el).removeAttr("nav-sm");
                        $(this.el).removeAttr("nav-top");
                        if (this.settings.size === "small") {
                            $(this.el).attr("nav-sm", "");
                        } else if (this.settings.size === "top") {
                            $(this.el).attr("nav-top", "");
                        }
                    } else {
                        throw new Error("错误的参数,仅支持{string} normal|small|top");
                    }
                },
                //填充组件结构
                initHtml: function initHtml() {
                    var html = "\n                <div class=\"sideNav-wrap\">\n                {{if data.sysName||data.syslogo}}\n                    <div class=\"sideNav-head\">\n                        {{if data.syslogo}}<div class=\"logo\">{{#data.syslogo}}</div>{{/if}}\n                        <div class=\"title\">{{data.sysName}}</div>\n                    </div>\n                {{/if}}\n            <div class=\"sideNav-body\">\n            {{each data.data as item index}}\n                <div class=\"sideNav-item-1\" data-index={{item.id}} data-id={{item.id}} data-sm-id={{item.id}}>\n                    <div class=\"content-box\">\n                        {{if item.ico && item.ico!=\"\"}}<div class=\"ico-item\"><i class=\"{{item.ico}}\"></i></div>{{/if}}\n                        <div class=\"text\">{{item.name}}</div>\n                        {{if item.child && item.child.length>0}} <i class=\"chevron\"></i>{{/if}}\n                    </div>\n                    {{if item.child && item.child.length>0}}\n                    <div class=\"sideNav-wrap-2\">\n                    <div class=\"sm-title\">{{item.name}}</div>\n                    {{each item.child as item2}}\n                    <div class=\"sideNav-item-2\" data-id={{item2.id}}>\n                        <div class=\"content-box\">\n                            {{if item2.ico && item2.ico!=\"\"}} <div class=\"ico-item\"><i class=\"{{item2.ico}}\"></i></div>{{/if}}\n                            <div class=\"text\">{{item2.name}}</div>\n                            {{if item2.child && item2.child.length>0}} <i class=\"chevron\"></i>{{/if}}\n                        </div>\n                        {{if item2.child && item2.child.length>0}}\n                        <div class=\"sideNav-wrap-3\">\n                        {{each item2.child as item3}}\n                            <div class=\"sideNav-item-3\" data-id={{item3.id}}>\n                                <div class=\"content-box\">\n                                    <div class=\"text\">{{item3.name}}</div>\n                                </div>\n                            </div>\n                        {{/each}}\n                        </div>\n                        {{/if}}\n                    </div>\n                    {{/each}}\n                    </div>\n                    {{else}}\n                    <div class=\"sideNav-wrap-2 no-child\">\n                    <div class=\"sm-title\">{{item.name}}</div>\n                    </div>\n                    {{/if}}\n                </div>\n            {{/each}}\n            </div>\n            <div class=\"sideMore\">\n            <div class=\"sideMore-text\"><i>...</i></div>\n            <div class=\"sideMore-wrap\"></div>\n        </div>\n                      <div class=\"sideNav-extend\">\n            {{#data.extend}}\n            </div>\n            </div>\n            <div class=\"sideNav-child-wrap\">\n                {{each data.data as item}}\n                <div class=\"sideNav-child\" data-index={{item.id}}>\n                    <div class=\"sm-title\">{{item.name}}</div>\n                    {{if item.child && item.child.length>0}}\n                    {{each item.child as item2}}\n                    <div class=\"sideNav-child2\" data-sm-id={{item2.id}}>\n                        <div class=\"content-box\">\n                            {{if item2.ico && item2.ico!=\"\"}} <div class=\"ico-item\"><i class=\"{{item2.ico}}\"></i></div>{{/if}}\n                            <div class=\"text\">{{item2.name}}</div>\n                            {{if item2.child && item2.child.length>0}} <i class=\"chevron\"></i>{{/if}}\n                        </div>\n                        {{if item2.child && item2.child.length>0}}\n                        <div class=\"sideNav-wrap-3\">\n                            {{each item2.child as item3}}\n                            <div class=\"sideNav-child3\" data-sm-id={{item3.id}}>\n                                <div class=\"content-box\">\n                                    <div class=\"text\">{{item3.name}}</div>\n                                </div>\n                            </div>\n                            {{/each}}\n                        </div>\n                        {{/if}}\n                    </div>\n                    {{/each}}\n                    {{/if}}\n                </div>\n                {{/each}}\n            </div>";
                    var tplHtml = template.compile(html);
                    $(this.el).html(tplHtml({
                        "data": this.settings
                    }));
                },
                data: function data(param) {
                    var data = param[1];
                    this.settings = $.extend(true, this.settings, data);
                    console.log(this.settings);
                    this.init();
                },
                //设置组件的默认选中项
                setActive: function setActive(id) {
                    if (typeof id != "string") {
                        id = id[1];
                    }
                    if (typeof $(this.el).attr("nav-sm") === "undefined") {
                        $(this.el).find("[data-id=\"" + id + "\"]").click();
                        if (this.settings.currentOpen && this.settings.size !== "top") {
                            $(this.el).find("[data-id=\"" + id + "\"]").parents(".sideNav-item-2").addClass("open");
                            $(this.el).find("[data-id=\"" + id + "\"]").parents(".sideNav-item-1").addClass("open");
                        }
                    } else {
                        $(this.el).find("[data-sm-id=\"" + id + "\"]").click();
                    }
                },
                //设置组件的主题
                setTheme: function setTheme(data) {
                    this.settings.theme = data[1];
                    this.changeTheme();
                },
                //设置布局
                setLayout: function setLayout(data) {
                    this.settings.size = data[1];
                    this.init();
                },
                //设置大小
                setSize: function setSize() {
                    var el = this.el;
                    var that = this;
                    el.find(".open").removeClass("open");
                    if (typeof $(el).attr("nav-sm") === "undefined") {
                        //现在为小侧边栏
                        $(el).attr("nav-sm", "");
                        $(el).addClass("transformtime");
                        setTimeout(function () {
                            $(el).removeClass("transformtime");
                            that.setPosition();
                        }, 300);
                        el.find(".icoSize").removeClass("aidicon-format-indent-decrease").addClass("aidicon-format-indent-increase");
                        return {
                            sizeStatus: "small"
                        };
                    } else {
                        //现在为大侧边栏
                        $(el).removeAttr("nav-sm");
                        el.find(".icoSize").removeClass("aidicon-format-indent-increase").addClass("aidicon-format-indent-decrease");
                        return {
                            sizeStatus: "normal"
                        };
                    }
                },
                //设置组件的logo
                setLogo: function setLogo(data) {
                    $(this.el).find(".sideNav-head .logo").html(data[1]);
                },
                //集中初始化子菜单ui位置
                setPosition: function setPosition() {
                    var left = $(this.el).width();
                    var top;
                    var el = this.el;
                    if (this.settings.data) {
                        this.settings.data.forEach(function (item) {
                            var index = item.id;
                            var $item = el.find(".sideNav-child[data-index=" + index + "]");
                            //在侧边小导航下
                            if (typeof $(el).attr("nav-sm") !== "undefined") {
                                top = el.find(".sideNav-item-1[data-index=" + index + "]").position().top;
                            }
                            //在顶部导航下
                            else if (typeof $(el).attr("nav-top") !== "undefined") {
                                left = el.find(".sideNav-item-1[data-index=" + index + "]").position().left;
                                top = 64;
                            }
                            $item.css({
                                "left": left,
                                "top": top
                            });
                        });
                    }
                },
                //监听顶部菜单在距离不够时出现更多按钮
                watchWidth: function watchWidth(len) {
                    var option = this.settings.data;
                    var el = this.el;
                    $(el).find(".sideNav-body").scrollLeft(1); //控制滚动条下移1px
                    var hideDom = $(el).find(".sideNav-body").find(".sideNav-item-1").eq(len - 1);
                    if ($(el).find(".sideNav-body").scrollLeft() > 0) {
                        $(el).find(".sideMore-wrap").prepend(hideDom.clone());
                        hideDom.hide();
                        if (hideDom.find(".active").length > 0) {
                            $(el).find(".sideMore").addClass("actived");
                        }
                        $(el).find(".sideMore").show();
                        this.watchWidth(len - 1);
                    }
                    $(el).find(".sideNav-body").scrollLeft(0); //滚动条返回顶部
                },
                //监听组件被选中时的数据对象
                watchActive: function watchActive() {
                    var value;
                    var that = this;
                    var el = this.el;
                    Object.defineProperty(this.active, "data", {
                        get: function get() {
                            return value;
                        },
                        set: function set(val) {
                            function setSatus(data) {
                                $(el).find(".active").removeClass("active");
                                $(el).find(".actived").removeClass("actived");
                                $(el).find("[data-id=\"" + data.id + "\"]").addClass("active");
                                $(el).find("[data-sm-id=\"" + data.id + "\"]").addClass("active");
                                // if (data.parentId !== "UAPI") {
                                if (data.parentId !== that.systemId) {
                                    // var parentId = parseInt(data.parentId);
                                    var parentId = data.parentId;
                                    var parentTop = that.dataMap[parentId].parentId;
                                    $(el).find("[data-id=\"" + parentId + "\"]").addClass("actived");
                                    $(el).find("[data-sm-id=\"" + parentId + "\"]").addClass("actived");
                                    $(el).find("[data-id=\"" + parentTop + "\"]").addClass("actived");
                                    $(el).find("[data-sm-id=\"" + parentTop + "\"]").addClass("actived");
                                }
                                if ($("[data-id=\"" + data.id + "\"]").parents(".sideMore-wrap").length === 1) {
                                    $(el).find(".sideMore").addClass("actived");
                                }
                            }

                            setSatus(val);
                            value = val;
                        }
                    });
                },
                //监听组件行为
                watchAction: function watchAction() {
                    var el = this.el; //组件实例容器
                    var option = this.settings;
                    var that = this;
                    //插件加载完成
                    el.on("load.bs", function (e, current) {
                    });
                    //插件选择结束
                    el.on("active.bs", function (e, current) {
                        // if (typeof current === "string") {
                        //     current = parseInt(current);
                        // }
                        that.active["data"] = that.dataMap[current];
                        el.trigger("done.bs", that.dataMap[current]);
                    });
                    //插件选择完成
                    el.on("done.bs", function (e, current) {
                    });
                    // 添加结果集到指定位置事件
                    el.on("insert.bs", function (e) {
                    });

                    //切换大小实例
                    el.on("resize.bs", function (e) {
                        e.sizeStatus = that.setSize().sizeStatus;
                    });

                    //重置实例
                    el.on("reset.bs", function (e) {
                    });
                },
                //改变组件主题
                changeTheme: function changeTheme() {
                    if (this.settings.theme > 7 && this.settings.theme < 0) {
                        throw new Error("主题只支持[0-7]的切换");
                    }
                    $(this.el).attr("theme", this.settings.theme.toString());
                    $(this.el).find(".sideNav-child-wrap").attr("theme", this.settings.theme.toString());
                },

                //注册组件事件
                initEvens: function initEvens() {
                    var el = this.el; //组件实例容器
                    var that = this;
                    var option = this.settings;
                    var timer = 0;
                    //切换大小
                    el.on("click", ".switch-size", function (e) {
                        el.trigger("resize.bs");
                        if ($("#backIndex").css("display") == "none") {
                            $("#backIndex").fadeIn();
                        } else {
                            $("#backIndex").css("display", "none");
                        }
                    });
                    //点击一级菜单
                    el.on("click", ".sideNav-item-1", function (e) {
                        var id;
                        //点击大侧边栏
                        if (typeof $(el).attr("nav-sm") === "undefined") {
                            id = $(this).attr("data-id");
                        } else {
                            id = $(this).attr("data-sm-id");
                        }
                        //有子项，打开二级菜单
                        if ($(this).find(".content-box").length > 1) {
                            //关闭状态
                            if ($(this).hasClass("open") === false || typeof $(el).attr("nav-sm") !== "undefined") {
                                el.find(".sideNav-item-1").removeClass("open");
                                $(this).addClass("open");
                            } else {
                                //打开状态
                                $(this).removeClass("open");
                            }
                        } else {
                            //没有子项，直接选中
                            el.trigger("active.bs", id);
                        }
                    });
                    //点击二级菜单
                    el.on("click", ".sideNav-item-2", function (e) {
                        var id = $(this).attr("data-id");
                        e.stopPropagation();
                        //有子项，打开三级菜单
                        if ($(this).find(".content-box").length > 1) {
                            if ($(this).hasClass("open") === false) {
                                el.find(".sideNav-item-2.open").removeClass("open");
                                $(this).addClass("open");
                            } else {
                                $(this).removeClass("open");
                            }
                        } else {
                            el.trigger("active.bs", id);
                        }
                    });
                    //点击二级菜单(小)
                    el.on("click", ".sideNav-child2", function (e) {
                        e.stopPropagation();
                        var id = $(this).attr("data-sm-id");
                        if ($(this).find(".content-box").length == 1) {
                            el.trigger("active.bs", id);
                        }
                    });
                    //点击三级菜单
                    el.on("click", ".sideNav-item-3", function (e) {
                        e.stopPropagation();
                        var id = $(this).attr("data-id");
                        el.trigger("active.bs", id);
                    });
                    //点击三级菜单(小)
                    el.on("click", ".sideNav-child3", function (e) {
                        e.stopPropagation();
                        var id = $(this).attr("data-sm-id");
                        el.trigger("active", id);
                    });
                    //移入一级菜单
                    el.on("mouseenter", ".sideNav-body>.sideNav-item-1>.content-box", function (e) {
                        var that = this;
                        timer = setTimeout(function () {
                            if (typeof $(el).attr("nav-sm") !== "undefined") {
                                el.find(".open").removeClass("open");
                                el.find(".fadeIn").removeClass("fadeIn");
                                $(that).parent().addClass("open");
                                var index = $(that).parent().attr("data-index");
                                var $item = $(".sideNav-child[data-index=" + index + "]");
                                $item.addClass("fadeIn");
                            } else if (typeof $(el).attr("nav-top") !== "undefined") {
                                el.find(".open").removeClass("open");
                                el.find(".fadeIn").removeClass("fadeIn");
                                $(that).parent().addClass("open");
                                var index = $(that).parent().attr("data-index");
                                var $item = $(".sideNav-child[data-index=" + index + "]");
                                $item.addClass("fadeIn");
                            }
                            ;
                        }, 200);
                    });
                    //移出一级菜单
                    el.on("mouseleave", ".sideNav-body>.sideNav-item-1>.content-box", function (e) {
                        clearTimeout(timer);
                        if (typeof $(el).attr("nav-sm") !== "undefined") {
                            if ($(e.toElement).hasClass("sideNav-body") === false && $(e.toElement).parents(".sideNav-child").length === 0) {
                                var $item = $(".sideNav-child");
                                $item.removeClass("fadeIn");
                                $(this).parents(".sideNav-item-1").removeClass("open");
                            }
                        }
                        if (typeof $(el).attr("nav-top") !== "undefined") {
                            if ($(e.toElement).parents(".sideNav-child").length === 0) {
                                var $item = $(".sideNav-child");
                                $item.removeClass("fadeIn");
                                $(this).parents(".sideNav-item-1").removeClass("open");
                            }
                        }
                    });

                    //移入二级菜单菜单(小)
                    el.on("mouseenter", ".sideNav-child2", function (e) {
                        if ($(this).find(".content-box").length > 1) {
                            $(this).addClass("open");
                        }
                    });

                    //移出二级子菜单菜单(小)
                    el.on("mouseleave", ".sideNav-child2", function (e) {
                        $(this).removeClass("open");
                    });

                    //移出二级菜单(小)
                    el.on("mouseleave", ".sideNav-child", function (e) {
                        $(".sideNav-item-1").removeClass("open");
                        $(".fadeIn").removeClass("fadeIn");
                    });

                    //移入更多菜单
                    el.on("mouseenter", ".sideMore", function (e) {
                        var that = this;
                        timer = setTimeout(function () {
                            $(that).addClass("open");
                        }, 200);
                    });
                    //移出更多菜单
                    el.on("mouseleave", ".sideMore", function (e) {
                        clearTimeout(timer);
                        $(".open").removeClass("open");
                        $(this).removeClass("open");
                    });

                    //移入更多菜单中的一级菜单
                    el.on("mouseenter", ".sideMore-wrap .sideNav-item-1>.content-box", function (e) {
                        $(this).parent().addClass("open");
                    });
                    el.on("mouseenter", ".sideMore-wrap .sideNav-item-1>.content-box", function (e) {
                        $(".sideNav-item-1").removeClass("open");
                        $(this).parent().addClass("open");
                    });
                    //移入更多菜单中的二级菜单
                    el.on("mouseenter", ".sideMore-wrap .sideNav-item-2", function (e) {
                        $(this).addClass("open");
                    });

                    //移出更多菜单中的二级菜单
                    el.on("mouseleave", ".sideMore-wrap .sideNav-item-2", function (e) {
                        $(this).removeClass("open");
                    });

                    el.find(".sideNav-body").scroll(function () {
                        debounce(function () {
                            that.setPosition();
                        }, 1000);
                    });

                    $(window).on("resize", function () {
                        debounce(function () {
                            $(el).find(".sideNav-body").find(".sideNav-item-1").show();
                            // $(el).find(".open").removeClass("open");
                            $(el).find(".sideMore-wrap").html("");
                            $(el).find(".sideMore").removeClass("actived");
                            $(el).find(".sideMore").hide();
                            var len = option.data.length;
                            that.watchWidth(len);
                            that.setPosition();
                        }, 500);
                    });
                }
            };
            return SideNav;
        }();
        $.fn.sideNav = function (options) {
            var ags = arguments;
            return this.each(function (index, item) {
                var el = $(item);
                var instance = el.data("sideNav");
                //判断实例是否存在
                if (!instance) {
                    instance = new SideNav(el, options);
                    //存放实例
                    el.data("sideNav", instance);
                }
                //外部可以通过$(select).MultipleRowFilter("init")
                if ($.type(options) === "string") {
                    return instance[options](ags);
                }
            });
        };
        $.fn.sideNav.data = {};

        //插件内部初始化，在目标元素上添加data-multipleRowFilter
        $(function () {
            $("[data-sideNav]").sideNav();
        });

        function debounce(fn, wait) {
            var timeout;
            return function () {
                if (timeout !== null) {
                    clearTimeout(timeout);
                }
                ;
                timeout = setTimeout(function () {
                    fn();
                }, wait);
            }();
        }

        //递归数组
        function flatten(data) {
            var list = [];
            data.forEach(function (item) {
                list.push(item);
                if (item.child && item.child.length > 0) {
                    list = [].concat(_toConsumableArray(list), _toConsumableArray(flatten(item.child)));
                }
            });
            return list;
        }

        //数组转map
        function arraytomap(data) {
            var map = {};
            data.forEach(function (item) {
                map[item.id] = item;
            });
            return map;
        }
    })(jQuery);
});